package com.xce.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.xce.order.entity.Order;
import com.xce.order.feign.ProductFeignClient;
import com.xce.order.service.OrderService;
import com.xce.product.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

/**
 * @ClassName OrderServiceImpl
 * @Description TODO
 * @Author lld
 * @Date 2025/6/25 1:08
 * @Version 1.0
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    private final RestTemplate restTemplate;
    private final DiscoveryClient discoveryClient;

    private LoadBalancerClient loadBalancerClient;

    private ProductFeignClient productFeignClient;

    @Autowired
    public OrderServiceImpl(RestTemplate restTemplate,DiscoveryClient discoveryClient) {
        this.restTemplate = restTemplate;
        this.discoveryClient = discoveryClient;
    }

    @Autowired
    private void setLoadBalancerClient(LoadBalancerClient loadBalancerClient, ProductFeignClient productFeignClient) {
        this.loadBalancerClient = loadBalancerClient;
        this.productFeignClient = productFeignClient;
    }
    @SentinelResource(value = "createOrder", fallback = "createOrderFallback")
    @Override
    public Order createOrder(Long productId, Long userId) {
        Order order = new Order();
        order.setId(new Random().nextLong());
        Product product = remoteProductWithFeign(productId);

        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickName("lld");
        order.setAddress("北京市朝阳区");
        order.setProductList(List.of(product));
        return order;

    }

    public Order createOrderFallback(Long productId, Long userId, Throwable ex) {
        Order order = new Order();
        order.setId(0L);
        order.setTotalAmount(new BigDecimal(0));
        order.setUserId(userId);
        order.setNickName("未知用户");
        order.setAddress("异常信息："+ex.getClass());
        return order;

    }
    private Product remoteProduct(Long productId) {
        ServiceInstance instance = discoveryClient.getInstances("service-product").get(0);
        String url = "http://"+instance.getHost()+":"+instance.getPort()+"/getProduct/"+ productId;
        log.info("发起远程调用：{}",url);
        return restTemplate.getForObject(url, Product.class);
    }

    // 随机调用负载均衡
    private Product remoteProductWithLoadBalancer(Long productId) {
        ServiceInstance instance = loadBalancerClient.choose("service-product");
        String url = "http://"+instance.getHost()+":"+instance.getPort()+"/getProduct/"+ productId;
        log.info("发起远程调用：{}",url);
        return restTemplate.getForObject(url, Product.class);
    }


    // 进阶：注解方式实现远程调用
    private Product remoteProductWithLoadBalancedAnnoation(Long productId) {

        String url = "http://service-product/getProduct/"+ productId;
        return restTemplate.getForObject(url, Product.class);
    }



    private Product remoteProductWithFeign(Long productId) {

        return productFeignClient.getProductById(productId);
    }
}
